package com.od.controller;

import com.od.entity.Enterprise;
import com.od.entity.Seeker;
import com.od.service.EnterpriseService;
import com.od.service.SeekerService;
import com.od.utils.JwtUtil;
import com.od.utils.UserUtil;

import jakarta.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@RestController
@CrossOrigin
public class LoginController {
    @Autowired
    private SeekerService seekerService;
    @Autowired
    private EnterpriseService enterpriseService;

    @RequestMapping("loginSeeker")
    public UserUtil loginSeeker(@RequestBody Seeker seeker) {
        System.out.println(seeker.getTel() + seeker.getPassword());
        Seeker loginSeeker = seekerService.getSeekerByTel(seeker.getTel());
        if (loginSeeker != null && loginSeeker.getPassword().equals(seeker.getPassword())) {
            UserUtil userUtil = new UserUtil();
            userUtil.setTel(seeker.getTel());
            userUtil.setPssword(seeker.getPassword());
            userUtil.setToken(JwtUtil.createToken());
            return userUtil;
        }
        return null;
    }

    @RequestMapping("loginEnterprise")
    public String loginEnterprise(@RequestBody Enterprise enterprise) {
        System.out.println(enterprise.getTel() + enterprise.getPassword());
        Enterprise loginEnterprise = enterpriseService.getEnterpriseByTel(enterprise.getTel());
        if (loginEnterprise != null && loginEnterprise.getPassword().equals(enterprise.getPassword())) {
            return "ok";
        } else {
            return "false";
        }
    }

    @GetMapping("checkToken")
    @ResponseBody
    public boolean getMethodName(HttpServletRequest request) {
        String token = request.getHeader("token");
        System.out.println(token);
        System.out.println(JwtUtil.checkToken(token));
        return JwtUtil.checkToken(token);
    }
    

}
